package com.weilele.leisure.time.ui.module.video_detail

import com.weilele.leisure.time.base.BaseVideoViewModel
import com.weilele.leisure.time.bean.VideoDetailBean
import com.weilele.leisure.time.bean.VideoListBean
import com.weilele.leisure.time.net.ApiInstance2
import com.weilele.mvvm.base.livedata.createStatusLiveData
import com.weilele.mvvm.base.livedata.data
import com.weilele.mvvm.base.livedata.success


class VideoDetailViewModel : BaseVideoViewModel() {
    /**
     * 设置播放器帮助类
     */
    val playerHelperData by lazy {
        createStatusLiveData<VideoDetailPlayerHelper>()
    }
    private val playerHelper
        get() = playerHelperData.data

    /**
     * 播放列表发生变化
     */

    val playListChangeData by lazy {
        createStatusLiveData<Pair<String?/*列表的key*/, Int/*当前position*/>>()
    }

    /**
     * 获取视频详情
     */
    val videoDetailData by lazy { createStatusLiveData<VideoDetailBean>() }
    fun getVideoDetail(dto: VideoListBean.RowsDTO) {
        videoDetailData.requestVideoApi {
            ApiInstance2.getVideoDetail(dto.id)
        }
    }

    fun startPlay(
        video: VideoDetailBean?,
        playerList: VideoDetailBean.PlayersDTO?,
        urls: MutableList<VideoDetailBean.PlayersDTO.DatasDTO>?,
        position: Int = 0,
        progress: Long = 0,
    ) {
        if (playListChangeData.data?.first == playerList?.key &&
            playListChangeData.data?.second == position
        ) {
            //如果是相同列表相同位置，直接返回
            return
        }
        playListChangeData.success(Pair(playerList?.key, position))
        playerHelper?.startPlay(video, playerList, urls, position, progress)
    }
}